package com.ruoyi.xp.order.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.common.constant.GlobalConstant;
import com.ruoyi.xp.common.constant.XpConstants;
import com.ruoyi.xp.order.domain.OrderItem;
import com.ruoyi.xp.order.service.IOrderItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.xp.order.mapper.OrderIncomeMapper;
import com.ruoyi.xp.order.domain.OrderIncome;
import com.ruoyi.xp.order.service.IOrderIncomeService;
import com.ruoyi.common.core.text.Convert;

/**
 * 虾皮订单收入信息Service业务层处理
 * 
 * @author chdx
 * @date 2022-12-04
 */
@Service
public class OrderIncomeServiceImpl implements IOrderIncomeService 
{
    @Autowired
    private OrderIncomeMapper orderIncomeMapper;

    @Autowired
    private IOrderItemService orderItemService;

    /**
     * 查询虾皮订单收入信息
     * 
     * @param orderIncomeId 虾皮订单收入信息主键
     * @return 虾皮订单收入信息
     */
    @Override
    public OrderIncome selectOrderIncomeByOrderIncomeId(Long orderIncomeId)
    {
        return orderIncomeMapper.selectOrderIncomeByOrderIncomeId(orderIncomeId);
    }

    /**
     * 查询虾皮订单收入信息列表
     * 
     * @param orderIncome 虾皮订单收入信息
     * @return 虾皮订单收入信息
     */
    @Override
    public List<OrderIncome> selectOrderIncomeList(OrderIncome orderIncome)
    {
        return orderIncomeMapper.selectOrderIncomeList(orderIncome);
    }

    /**
     * 新增虾皮订单收入信息
     * 
     * @param orderIncome 虾皮订单收入信息
     * @return 结果
     */
    @Override
    public int insertOrderIncome(OrderIncome orderIncome)
    {
        return orderIncomeMapper.insertOrderIncome(orderIncome);
    }

    /**
     * 修改虾皮订单收入信息
     * 
     * @param orderIncome 虾皮订单收入信息
     * @return 结果
     */
    @Override
    public int updateOrderIncome(OrderIncome orderIncome)
    {
        return orderIncomeMapper.updateOrderIncome(orderIncome);
    }

    /**
     * 批量删除虾皮订单收入信息
     * 
     * @param orderIncomeIds 需要删除的虾皮订单收入信息主键
     * @return 结果
     */
    @Override
    public int deleteOrderIncomeByOrderIncomeIds(String orderIncomeIds)
    {
        return orderIncomeMapper.deleteOrderIncomeByOrderIncomeIds(Convert.toStrArray(orderIncomeIds));
    }

    /**
     * 删除虾皮订单收入信息信息
     * 
     * @param orderIncomeId 虾皮订单收入信息主键
     * @return 结果
     */
    @Override
    public int deleteOrderIncomeByOrderIncomeId(Long orderIncomeId)
    {
        return orderIncomeMapper.deleteOrderIncomeByOrderIncomeId(orderIncomeId);
    }

    /**
     * 更新采购价及利润
     *
     * @param orderSn
     */
    @Override
    public void updatePurchasePriceAndProfit(String orderSn) {
        OrderIncome orderIncomeQuery = new OrderIncome();
        orderIncomeQuery.setOrderSn(orderSn);
        List<OrderIncome> orderIncomeList = orderIncomeMapper.selectOrderIncomeList(orderIncomeQuery);
        for (OrderIncome orderIncome : orderIncomeList) {
            OrderItem orderItemQuery = new OrderItem();
            orderItemQuery.setOrderSn(orderIncome.getOrderSn());
            List<OrderItem> orderItemList = orderItemService.selectOrderItemList(orderItemQuery);
            BigDecimal purchasePrice = new BigDecimal(0);
            for (OrderItem orderItem : orderItemList) {
                purchasePrice = purchasePrice.add(orderItem.getPurchasePrice() == null ? new BigDecimal(0) : orderItem.getPurchaseTotalPrice());
            }

            BigDecimal escrowAmount = orderIncome.getEscrowAmount();
            BigDecimal orderProfit = escrowAmount.multiply(new BigDecimal(XpConstants.RATE_MY)).subtract(purchasePrice).
                    subtract(orderIncome.getWarehousePackageAmount() == null ? new BigDecimal(0) : orderIncome.getWarehousePackageAmount());

            orderIncome.setPurchasePrice(purchasePrice);
            orderIncome.setOrderProfit(orderProfit);

            orderIncomeMapper.updateOrderIncome(orderIncome);
        }
    }

    /**
     * 查询没有货代打包费用的订单
     *
     * @return
     */
    @Override
    public List<OrderIncome> selectOrderHaveNoWarehousePackageAmount() {
        return orderIncomeMapper.selectOrderHaveNoWarehousePackageAmount();
    }
}
